package com.example.demo.dao;


import com.example.demo.bean.GoodsSeckill;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface GoodsDao {
    @Select("select g.*,gs.seckill_price,gs.stock_count from sk_goods g join sk_goods_seckill gs on g.id=gs.goods_id ")
    public List<GoodsSeckill> findGoodsSeckill();


    @Select("select g.*,gs.* from sk_goods g join sk_goods_seckill gs on " +
            "g.id=gs.goods_id where g.id=#{goodsId}")
    public GoodsSeckill findGoodsSeckillAllInfro(Long goodsId);

    @Update("update sk_goods set goods_sock=goods_stock-1 where id=#{id} and goods_stock>0")
    public Integer updateGoods(Long id);

    @Update("update sk_goods_seckill set stock_count=stock_count-1 where id=#{id} and stock_count>0")
    public Integer updateGoodsSeckill(Long id);


    @Select("select version from sk_goods_seckill where goods_id=#{goodsId}")
    public Integer findVersion(Long goodsId);

    @Update("update sk_goods_seckill set stock_count=stock_count-1,version=version+1 where goods_id=#{goodsId} and stock_count>0 and version=#{version}")
    public Integer updateStockCount(GoodsSeckill goodsSeckill1);
}
